package com.example.springboott1.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboott1.entity.AdminUser;
import com.example.springboott1.entity.User;
import com.example.springboott1.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //登录
    @PostMapping("login")
    public User login(@RequestBody User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername()).eq("password",user.getPassword());
        User user11= userService.getOne(queryWrapper);

        return user11;
    }

    //导出接口
    @GetMapping("/export")
    public void export(HttpServletResponse response)throws Exception{
        List<User> list = userService.list();
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.addHeaderAlias("username","用户名");
        writer.addHeaderAlias("password","密码");
        writer.addHeaderAlias("nickname","昵称");
        writer.addHeaderAlias("email","邮箱");
        writer.addHeaderAlias("phone","电话");
        writer.addHeaderAlias("address","地址");
        writer.addHeaderAlias("createTime","创建时间");

        writer.merge(7,"用户名单");

        writer.write(list,true);

        //写出xls格式
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
        // 设置浏览器响应的格式
        response.setHeader("Content-Disposition","attachment;filename=用户名单.xls");

        /*
        //写出xlsx格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=test.xlsx");
        */

        ServletOutputStream out=response.getOutputStream();

        writer.flush(out,true);

        writer.close();

        //此处记得关闭输出Servlet流
        IoUtil.close(out);

    }

    //批量删除
    @DeleteMapping("/delalldataofpeople")
    public boolean delall(@RequestBody String allpws){
        JSONObject json = JSONObject.fromObject(allpws);
        JSONArray array = json.getJSONArray("allpws");
        List<String> idList = new ArrayList();
        for (int i=0;i<array.size();i++){
            idList.add(array.getString(i));
        }
        return userService.removeByIds(idList);
    }

    //新增和修改
//    @PostMapping("/")
//    public boolean save(@RequestBody User user) {
//        //新增或者更新
//        return userService.saveOrUpdate(user);
//    }
    //这里的新增和修改应该区分开来，后端管理的时候要做区分。
    //新增和修改
    @PostMapping("/")
    public boolean save(@RequestBody User user) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        Long count= userService.count(queryWrapper);
        if(count>0){
            return false;
        }
        //根据有无id，新增或修改
        return userService.saveOrUpdate(user);
    }

    //自定义mybatis条件查询
//    @GetMapping("/findST")    //条件查询
//    public List<User> findByST(@RequestParam(required = false) String username, @RequestParam(required = false) String phone, @RequestParam(required = false) String address){
//        return userMapper.findST(username,phone,address);
//    }



    //单个删除
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id){
        return userService.removeById(id);
    }

    //分页查询数据,可带参可不带
    @GetMapping("/page")
    public IPage<User> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "13") Integer pageSize,@RequestParam( required = false) String username,@RequestParam(required = false) String phone,@RequestParam(required = false) String address){
        IPage<User> page = new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (username!=null){
            queryWrapper.like("username",username);
        }
        if(phone!=null){
            queryWrapper.like("phone",phone);
        }
        if (address!=null) {
            queryWrapper.like("address",address);
        }
        return userService.page(page,queryWrapper);
    }

    //模糊查询，不分页，已弃用
//    @GetMapping("/findParam")
//    public List<User> findParam(@RequestParam(required = false) String username,@RequestParam(required = false) String phone,@RequestParam(required = false) String address){
//        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        if (username!=null){
//            queryWrapper.like("username",username);
//        }
//        if(phone!=null){
//            queryWrapper.like("phone",phone);
//        }
//        if (address!=null) {
//            queryWrapper.like("address",address);
//        }
//        return userService.list(queryWrapper);
//    }
}
